/*
 * author      : 吴中勤
 * date        : 2018/8/20
 * description : moby 资金请求 -> sap的资金请求
 */

package com.deepexi.adapter.converter.sap.fund;

import com.deepexi.adapter.converter.sap.DateFormatConvert;
import com.deepexi.adapter.converter.sap.sapdict.SapDictService;
import com.deepexi.adapter.converter.sap.sapdict.SapDictType;
import com.deepexi.adapter.facade.moby.request.MobyEventBody;
import com.deepexi.adapter.facade.moby.request.MobyMember;
import com.deepexi.adapter.facade.moby.request.MobyPayInfo;
import com.deepexi.adapter.facade.moby.request.MobyRefundInfo;
import com.deepexi.adapter.sdk.sap.dto.fund.SapFinalPaymentRequestBody;
import com.deepexi.adapter.sdk.sap.dto.fund.SapFundRequestBody;
import com.deepexi.adapter.sdk.sap.dto.fund.SapFundResponseBody;
import com.deepexi.adapter.sdk.sap.dto.fund.SapRefundRequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FundAssembler {

    @Autowired
    private SapDictService dictService;

    // 构建一个 SapAdapterFund
    public SapAdapterFund fromSapResponse(
            MobyEventBody body,
            MobyPayInfo mobyPayInfo,
            SapFundResponseBody responseBody) {
        SapAdapterFund fund = new SapAdapterFund();

        fund.setShopCode(body.getMobyTradeOrder().getShopCode());
        fund.setMobyOrderId(body.getMobyTradeOrder().getId());
        fund.setMemberId(body.getMobyMember().getId());
        fund.setPayMoney(mobyPayInfo.getPayMoney());
        fund.setFundType(dictService.getSapValueByMoby(SapDictType.FUND_TYPE, mobyPayInfo.getFundType()));
        fund.setPayChannel(dictService.getSapValueByMoby(SapDictType.PAY_WAY, mobyPayInfo.getPayChannel()));
        fund.setPaidAt(mobyPayInfo.getPaidAt());
        fund.setPayOrderId(mobyPayInfo.getPayOrderNo());

        fund.setSapReceiptNo(responseBody.getSapReceiptNo());

        return fund;
    }


    // 将 MobyPayInfo -> SapFundRequestBody
    public SapFundRequestBody assemblePayInfo(
            final MobyEventBody body,
            final MobyPayInfo mobyPayInfo,
            final String sapOrderNo
    ) {
        MobyMember member = body.getMobyMember();
        SapFundRequestBody sapBody = new SapFundRequestBody();
        // set
        sapBody.setShopCode(body.getMobyTradeOrder().getShopCode());

        // 设置日期, 需要格式转换
        sapBody.setPaidAt(DateFormatConvert.getTimestamp(mobyPayInfo.getPaidAt()));
        sapBody.setAmount(mobyPayInfo.getPayMoney());
        String operatorId = mobyPayInfo.getOperatorId();
        sapBody.setOperatorId(null != operatorId ? operatorId : member.getId());


        sapBody.setMemberPhone(member.getPhone());
        sapBody.setOrderId(body.getMobyTradeOrder().getId());
        // TODO 订单标记 必须，1正单 使用固定值
        // TODO 参考订单号 不用
        sapBody.setMemberId(member.getId());
        sapBody.setFundType(dictService.getSapValueByMoby(SapDictType.FUND_TYPE, mobyPayInfo.getFundType()));
        sapBody.setPayOrderId(mobyPayInfo.getPayOrderNo());
        sapBody.setPayChannel(dictService.getSapValueByMoby(SapDictType.PAY_WAY, mobyPayInfo.getPayChannel()));
        sapBody.setSceneId(body.getMobyTradeOrder().getOrderTypeCode());
        sapBody.setBankNo(mobyPayInfo.getBankNo());
        // sap订单号
        if (null != sapOrderNo) sapBody.setSapOrderNo(sapOrderNo);

        return sapBody;
    }


    // 将 MobyPayInfo -> SapFundRequestBody
    public SapFinalPaymentRequestBody assembleFinalPayInfo(
            final MobyEventBody body,
            final MobyPayInfo mobyPayInfo,
            String sapOrderNo
    ) {
        MobyMember member = body.getMobyMember();
        SapFinalPaymentRequestBody sapBody = new SapFinalPaymentRequestBody();
        // set
        sapBody.setShopCode(body.getMobyTradeOrder().getShopCode());

        // 设置日期, 需要格式转换
        sapBody.setPaidAt(DateFormatConvert.getTimestamp(mobyPayInfo.getPaidAt()));
        sapBody.setAmount(mobyPayInfo.getPayMoney());
        String operatorId = mobyPayInfo.getOperatorId();
        sapBody.setOperatorId(null != operatorId ? operatorId : member.getId());
        sapBody.setMemberPhone(member.getPhone());
        sapBody.setOrderId(body.getMobyTradeOrder().getId());
        // TODO 订单标记 必须，1正单 使用固定值
        // TODO 参考订单号 不用
        sapBody.setMemberId(member.getId());
        sapBody.setFundType(dictService.getSapValueByMoby(SapDictType.FUND_TYPE, mobyPayInfo.getFundType()));
        sapBody.setPayOrderId(mobyPayInfo.getPayOrderNo());
        sapBody.setPayChannel(dictService.getSapValueByMoby(SapDictType.PAY_WAY, mobyPayInfo.getPayChannel()));
        sapBody.setBankNo(mobyPayInfo.getBankNo());
        // sap订单号
        if (null != sapOrderNo) sapBody.setSapOrderNo(sapOrderNo);

        return sapBody;
    }

    /** 组装退款信息 */
    public SapRefundRequestBody assembleRefund(
            MobyEventBody body,
            SapAdapterFund payRecord,
            MobyRefundInfo refundInfo) {

        SapRefundRequestBody sapBody = new SapRefundRequestBody();
        // from body
        sapBody.setShopCode(body.getMobyTradeOrder().getShopCode());
        sapBody.setMemberMobileNo(body.getMobyMember().getPhone());
        sapBody.setOrderId(body.getMobyTradeOrder().getId());
        sapBody.setMemberId(body.getMobyMember().getId());
        sapBody.setSapOrderNo("");

        // from payRecord
        sapBody.setPayChannel(dictService.getSapValueByMoby(SapDictType.PAY_WAY, payRecord.getPayChannel()));
        sapBody.setSapReceiptNo(payRecord.getSapReceiptNo());

        // from refundInfo
        sapBody.setOperatorId(refundInfo.getOperatorId());
        sapBody.setPayOrderNo(refundInfo.getPayOrderNo());
        sapBody.setRefundTime(DateFormatConvert.getTimestamp(refundInfo.getUpdatedAt()));
        sapBody.setRefundOrderId(refundInfo.getRefundOrderId());

        return sapBody;
    }
}
